﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rapid
{
    public partial class frmReportTemplate : frmMenustrip
    {
        RapidDataDataContext db = new RapidDataDataContext();

        IDictionary<int, TextBox> dicPatientInformation;

        public frmReportTemplate()
        {
            InitializeComponent();

            #region style for button
            btnCancel.TabStop = false;
            btnCancel.FlatStyle = FlatStyle.Flat;
            btnCancel.FlatAppearance.BorderSize = 0;
            btnCancel.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent

            btnApply.TabStop = false;
            btnApply.FlatStyle = FlatStyle.Flat;
            btnApply.FlatAppearance.BorderSize = 0;
            btnApply.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent
            #endregion

            lblTestdate.Text = "Date: " + DateTime.Now.ToString("MMMM") + " " + DateTime.Now.Day + ", " + DateTime.Now.Year;


            // facility
            if (db.Facilities.Count() > 0)
            {
                var facility = db.Facilities.OrderByDescending(f => f.Id).First();

                txtF_Doctor.Text = facility.Name;
                txtF_Address.Text = facility.Address;
                txtF_Country.Text = facility.Country;
                txtF_Phone.Text = facility.Phone;
                txtF_Fax.Text = facility.Fax;
                txtF_Email.Text = facility.Email;
            }

            dicPatientInformation = new Dictionary<int, TextBox>
            {
                {1, txtPatientTitle1},
                {2, txtPatientTitle2},
                {3, txtPatientTitle3},
                {4, txtPatientTitle4}
            };

            for (int i = 1; i <= 4; i++)
            {
                var x = db.PatientTitles.ToList();
                dicPatientInformation[i].Text = db.PatientTitles.Single(pt => pt.Id == i).Title;
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {            
            Facility facility = new Facility { 
                Name = txtF_Doctor.Text,
                Address = txtF_Address.Text,
                Country = txtF_Country.Text,
                Phone = txtF_Phone.Text,
                Fax = txtF_Fax.Text,
                Email = txtF_Email.Text
            };

            db.Facilities.InsertOnSubmit(facility);

            for (int i = 1; i <= 4; i++ )
            {
                db.PatientTitles.Single(pt => pt.Id == i).Title = dicPatientInformation[i].Text;
            }

            // Submit the change to the database. 
            try
            {
                db.SubmitChanges();
                MessageBox.Show("Changes have been successfully saved", "Success", MessageBoxButtons.OK);
            }
            catch
            {
                MessageBox.Show("Sorry can not save data at this time.", "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure want to cancel?", "Question", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                this.Hide();
                frmRapidRead _frmRapidRead = new frmRapidRead();
                _frmRapidRead.Show();
            }
        }
    }
}
